import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import { getAllAdmins,delAdmins,addAdmins } from '../api/adminReq'
import storageUtils from '../utils/storageUtils'


interface ModelState {
    adminData: listType[],
}

interface ModelsType {
    state: ModelState;
    effects: {
        gteAdmin: Effect;
        delOneAdmins:Effect;
        addOneAdmins:Effect;
    };
    reducers: {
        initGteAdmin: ImmerReducer
    }

}
type listType = {
	_id: string,
	password: string,
	account: string,
	realname: string,
	telephone: string,
	status: number
}

const adminModel: ModelsType = {

    state: {
        adminData: [],
    },
    effects: {
        *gteAdmin({ payload }, { call, put }) {
            const res = yield call(getAllAdmins)
            console.log(res);//这是获取到的结果
            if (res.code == 200) {
                yield put({
                    type: 'initGteAdmin',
                    payload:res.message
                })
            }
            
        },
        // 删除
        *delOneAdmins({ payload }, { call, put }){
            const res = yield call(()=>delAdmins(payload))
            console.log(res);//这是获取到的结果
            if(res.code==200){
                const result=  yield call(getAllAdmins)
                yield put({
                    type: 'initGteAdmin',
                    payload:result.message
                })
            }
        },
        // 新增
        *addOneAdmins({ payload }, { call, put }){
            const res = yield call(()=>addAdmins(payload))
            console.log(res);//这是获取到的结果
            if(res.code==200){
                const result=  yield call(getAllAdmins)
                yield put({
                    type: 'initGteAdmin',
                    payload:result.message
                })
            }
        }

    },
    reducers: {
        initGteAdmin(state, action) {
            // console.log(state, action);
            state.adminData = action.payload
        },
    },

};

export default adminModel;
